<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class ShoppingcartController extends Controller {
    public function shoppingcart(){
        return view('home\checkout');
    }
    public function shoppingadd(request $req , $goods_id){
    	$data[$goods_id]['number'] = $req->get('number');
    	$data[$goods_id]['goods_id'] = $goods_id;
    	$cartdata = $req->session()->get('shoppingcart');
	    if (isset($cartdata[$goods_id])) {
		    foreach ($cartdata as $v) {
			    if($goods_id == $v['goods_id']){
    				$cartdata[$goods_id]['number'] += $data[$goods_id]['number'];
    			}
		    }
		    $arr = $cartdata;
	    } else {
	    	if (is_array($cartdata)) {
		    	$arr = $cartdata + $data;
	    	} else {
	    		$arr = $data;
	    	}
	    }
    	$req->session()->put('shoppingcart' , $arr);
    	return redirect('single/'.$goods_id);
    }
    public function shoppingdel(){

    }
}
